﻿<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Reference.Apps"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Interactivity="using:Microsoft.Xaml.Interactivity" xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
    x:Class="Reference.Apps.MainPage"
    mc:Ignorable="d">
    <Page.Resources>
        <CollectionViewSource x:Key="SnippetsGroupedByCodeGroup" ItemsPath="Snippets" IsSourceGrouped="True" Source="{Binding Groups}"  />
        <Style x:Key="HeaderButtonStyle" TargetType="Button">
            <Setter Property="MinWidth" Value="0"/>
            <Setter Property="MinHeight" Value="0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ButtonBase">
                        <Grid Background="Transparent">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="PointerOver"/>
                                    <VisualState x:Name="Pressed"/>
                                    <VisualState x:Name="Disabled"/>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="FocusStates">
                                    <VisualState x:Name="Focused">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualWhite"/>
                                            <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualBlack"/>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Unfocused"/>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="CheckStates">
                                    <VisualState x:Name="Checked"/>
                                    <VisualState x:Name="Unchecked"/>
                                    <VisualState x:Name="Indeterminate"/>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <TextBlock x:Name="Text" Text="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" Style="{StaticResource SubheaderTextBlockStyle}" />
                            <TextBlock x:Name="Arrow" Grid.Column="1" Margin="10,0, 0,0" Text="&#x276d;" Foreground="{TemplateBinding Foreground}" Style="{StaticResource SubheaderTextBlockStyle}" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Page.Resources>
    <Page.BottomAppBar>
        <CommandBar RequestedTheme="Light">
            <AppBarButton x:Uid="W8RefreshAppBarButton" Icon="Refresh" Label="actualizar" Command="{Binding UpdateDataCommand, Mode=OneWay}"/>
        </CommandBar>
    </Page.BottomAppBar>

    <Page.DataContext>
        <Binding Mode="OneWay" Path="Main" Source="{StaticResource VMLocator}"/>
    </Page.DataContext>
    <Interactivity:Interaction.Behaviors>
        <Core:EventTriggerBehavior EventName="Loaded">
            <Core:InvokeCommandAction Command="{Binding GetDataCommand, Mode=OneWay}"/>
        </Core:EventTriggerBehavior>
    </Interactivity:Interaction.Behaviors>

    <Grid Background="{StaticResource ApplicationPageBackgroundBrush}">
    	<VisualStateManager.VisualStateGroups>
    		<VisualStateGroup x:Name="VisualStateGroup">
    			<VisualState x:Name="Default"/>
    			<VisualState x:Name="Snapped">
    				<Storyboard>
    					<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="searchBox">
    						<DiscreteObjectKeyFrame KeyTime="0">
    							<DiscreteObjectKeyFrame.Value>
    								<Visibility>Collapsed</Visibility>
    							</DiscreteObjectKeyFrame.Value>
    						</DiscreteObjectKeyFrame>
    					</ObjectAnimationUsingKeyFrames>
    				</Storyboard>
    			</VisualState>
    		</VisualStateGroup>
    	</VisualStateManager.VisualStateGroups>
        <Grid.ChildrenTransitions>
            <TransitionCollection>
                <EntranceThemeTransition/>
            </TransitionCollection>
        </Grid.ChildrenTransitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- Back button and page title -->
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="120"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock x:Uid="AppNameTextBlock" x:Name="pageTitle" Text="Reference" Style="{StaticResource HeaderTxtBlockStyle}" Grid.Column="1" 
				IsHitTestVisible="false" TextWrapping="NoWrap" VerticalAlignment="Bottom" Margin="0,0,30,40"  />
            <!-- This button will drag attention from the focus: -->
            <Button Opacity="0" />
            <SearchBox x:Name="searchBox" Grid.Column="1" Width="270" FocusOnKeyboardInput="True" IsTabStop="False" SearchHistoryEnabled="False"
                    Margin="0,30,100,0" HorizontalAlignment="Right" VerticalAlignment="Center" QuerySubmitted="SearchBox_QuerySubmitted" />
        </Grid>
        <GridView Grid.Column="1" Grid.Row="1" ItemTemplate="{StaticResource WCodeSnippetTemplate}" 
                  ItemsSource="{Binding Source={StaticResource SnippetsGroupedByCodeGroup}}" 
                  IsSwipeEnabled="False" SelectionMode="None" Padding="120,0,0,10" 
                  IsItemClickEnabled="True" ItemClick="SnippetsListItem_Click"
                  >
            <GridView.ItemsPanel>
                <ItemsPanelTemplate>
                    <ItemsWrapGrid />
                </ItemsPanelTemplate>
            </GridView.ItemsPanel>
            <GridView.GroupStyle>
                <GroupStyle HidesIfEmpty="True">
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <Grid Margin="0,0,220,0">
                                <Button Content="{Binding Name}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Style="{StaticResource HeaderButtonStyle}" Foreground="{StaticResource ApplicationContrastBackgroundBrush}" Click="HeaderButton_Click" />
                            </Grid>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>

                </GroupStyle>
            </GridView.GroupStyle>
        </GridView>
		<!-- Loading grid -->
		<Grid Grid.RowSpan="2" Background="{StaticResource ApplicationContrastBackgroundTransparetBrush}" Visibility="{Binding IsBusy, Converter={StaticResource BoolToVisibilityConverter}}">
			<Grid Background="{StaticResource ApplicationPageBackgroundBrush}" Height="200">
			<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Orientation="Horizontal"> 
				<ProgressRing IsActive="{Binding IsBusy}" Height="41"  Width="41" />
				<TextBlock VerticalAlignment="Center" Margin="10,0,0,0" Foreground="{StaticResource ApplicationContrastBackgroundBrush}" Style="{StaticResource SubheaderTextBlockStyle}">Cargando</TextBlock>
				</StackPanel>
			</Grid>
		</Grid>
    </Grid>
</Page>
